package com.example.lishop.controller

import com.example.lishop.base.MyResponse
import com.example.lishop.entity.BTestEntity
import com.example.lishop.entity.bean.BaseListBean
import com.example.lishop.exception.ExceptEnum
import com.example.lishop.service.impl.TestServiceImpl
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.hibernate.validator.constraints.Length
import org.springframework.beans.BeanUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.util.Assert
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*

/**
 * 测试接口
 */
@Api(value = "/Test", tags = ["测试接口"])
@RestController
@RequestMapping("/test")
@Validated
class TestCtl {

    @Autowired
    lateinit var service: TestServiceImpl

    @ApiOperation(value = "测试接口test")
    @RequestMapping(value = [""])
    fun testGet() : String{
        return "Hello , this is a get request ."
    }

    @ApiOperation(value = "测试接口,返回输入数据")
    @RequestMapping(value = ["/input/{input}"])
    fun testInput(@PathVariable input:String) : String{
        return "Hello , $input ."
    }

    @ApiOperation(value = "测试post接口")
    @RequestMapping(value = ["/post"], method = [RequestMethod.POST])
    fun testPost() : String{
        return "Hello , this is a post request ."
    }

    @ApiOperation(value = "添加数据")
    @PostMapping(value = ["/add"])
    fun add(@RequestParam @Length(min = 2, max = 10,message = "数据长度大于等于1，小于等于10") name:String) : MyResponse<BTestEntity> {
        val bean = BTestEntity()
        bean.name = name
        return MyResponse(200, "添加成功", service.add(bean))
    }

    @ApiOperation(value = "修改数据")
    @PostMapping(value = ["/update"])
    fun update(@RequestBody bean: BTestEntity) : MyResponse<BTestEntity> {
        service.update(bean)
        return MyResponse(200, "修改成功", detail(bean.id).data)
    }

    @ApiOperation(value = "数据列表")
    @PostMapping(value = ["/list"])
    fun list(@RequestBody bean: BaseListBean) : MyResponse<Page<BTestEntity>> {
        return MyResponse(200, "请求列表成功", service.list(bean.page, bean.size))
    }

    @ApiOperation(value = "删除数据")
    @PostMapping(value = ["/del"])
    fun del(@RequestParam id: Int) : MyResponse<Unit> {
        return MyResponse(200, "删除成功", service.del(id))
    }

    @ApiOperation(value = "查询数据")
    @PostMapping(value = ["/detail"])
    fun detail(@RequestParam id: Int) : MyResponse<BTestEntity> {
//        BeanUtils.copyProperties()
        return MyResponse(200, "查找成功", service.detail(id))
    }
}